local GuildWarRoundCountDown = class("GuildWarRoundCountDown")
local CrossServerGuildWarHelper = require("app.crossServer.views.guildWar.CrossServerGuildWarHelper")

function GuildWarRoundCountDown:ctor(params)
    local startH, startM, startS = CrossServerGuildWarHelper.getGuildWarGetTicketTime()
    local ticketTime = app:getServerTimeByHMSW(startH, startM, startS, nil, false)
    local startPrepareTime = tonumber(GD:getCSGuildWarPar("StartPrepareTime", "240"))
    self._startTime = ticketTime + startPrepareTime

    self._battlePrepareTime = tonumber(GD:getCSGuildWarPar("BattlePrepareTime", "60"))
    self._dataPrepareTime = tonumber(GD:getCSGuildWarPar("BattleCountdown", "15"))
    self._battleTime = tonumber(GD:getCSGuildWarPar("BattleTime", "90"))

    self._dataTime = self._battlePrepareTime + self._dataPrepareTime
    self._notShowNextTime = self._dataTime + 15 -- 添加一个 偏移时间 
    self._roundTime =  self._battlePrepareTime + self._dataPrepareTime + self._battleTime

    self._startTimeAddFirstBattlePrepareTime = self._startTime + self._battlePrepareTime
    self._curRound = 0
    if not params then
        params = {}
    end
    self._tickCallback = params.tickCallback
end

function GuildWarRoundCountDown:_calculateRound()
    local curTime = math.floor(app.session:getServerTime()) or os.time()
    -- 开始时间  =  开始时间  +  第一次战斗准备时间
    if curTime < self._startTimeAddFirstBattlePrepareTime then
        return 0, nil, self._startTimeAddFirstBattlePrepareTime - curTime
    end
    local stage = Enums.CSGuildWarRoundStage.Prepare

    local diffTime = curTime - self._startTime

    local round = math.floor( diffTime/self._roundTime ) + 1

    local stageTime = diffTime % self._roundTime
    local nextBattleCountDown = 0

    if stageTime < self._battlePrepareTime then
        stage = Enums.CSGuildWarRoundStage.Prepare
        nextBattleCountDown = self._dataTime - stageTime
    elseif stageTime < self._dataTime then
        stage = Enums.CSGuildWarRoundStage.Data
        nextBattleCountDown = self._dataTime - stageTime
    elseif stageTime < self._notShowNextTime then
        -- 开始战斗 偏移一段时间在显示下一轮战斗文本
        stage = Enums.CSGuildWarRoundStage.FightStart
        nextBattleCountDown = self._dataTime + self._roundTime - stageTime
    else
        -- 显示下一轮战斗
        stage = Enums.CSGuildWarRoundStage.FightEnd
        nextBattleCountDown = self._dataTime + self._roundTime - stageTime
    end
    
    return round, stage, nextBattleCountDown
end

function GuildWarRoundCountDown:startCountDown()
    if not self._schedulerIdx then
        self._schedulerIdx = scheduler.newCron(handler(self, self._tick), 0.5, true)
        self:_tick()
    end
end

function GuildWarRoundCountDown:_tick()
    local round, stage, nextBattTime = self:_calculateRound()
    local isStageChange = false
    if self._oldStage ~= stage then
        self._oldStage = stage
        isStageChange = true
    end
    
    -- local nextRoundString = string.format(L("CSGuildWarRoundCountDown"), nextBattTime)
    if self._tickCallback then
        self._tickCallback(round, stage, nextBattTime, isStageChange)
    end
end

function GuildWarRoundCountDown:clean()
    if self._schedulerIdx then
        scheduler.cancelCron(self._schedulerIdx)
        self._schedulerIdx = nil
    end
end

return GuildWarRoundCountDown